﻿@using ST = EmoneyWeiXinCommon.StringTools;
@{
    ViewBag.Title = "消息模板管理";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="right">
    <h3>消息模板管理</h3>
    <!--manage-->
    <div class="manage">
        <!--manage_top-->
        <div class="manage_top">
            <ul class="manage_top_ul">
            </ul>
            <div class="manage_register">
                <p><span>模板ID：</span><input id="modelid" type="text"><input style="display: none" id="hidid" type="text"></p>
                <p><span>模板标题：</span><input id="title" type="text"></p>
            </div>
            <div class="manage_register">
                <p class="manage_register_p">
                    <span>模板说明：</span>
                    <input id="memo" type="text">
                </p>
            </div>
            <div class="manage_register manage_register02">
                <p>
                    <span>模板内容：</span>
                    <textarea id="content"></textarea>
                </p>
            </div>
            <p class="manage_add"><a id="btnSave" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(2, 3, ViewBag.leftid))'>保存</a><a id="btnCancel" class="manage_add_font">取消</a></p>
        </div>
        <!--manage_top end-->
        <p class="manage_line"></p>
        <!--manage_bottom-->
        <div class="manage_bottom">
            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                <tr>
                    <th><span>编号</span></th>
                    <th><span>模板ID</span></th>
                    <th><span>模板标题</span></th>
                    <th><span>模板说明</span></th>
                    <th><span>操作</span></th>
                </tr>
                <tbody id="dvList"></tbody>
            </table>
            <div class="manage_page" id="manage_page">
            </div>
        </div>
        <!--manage_bottom end-->
    </div>
    <!--manage end-->
</div>

<script type="text/javascript">
    var weixinid = "";
    var pageIndex = 1;
    var pageSize = 10;
    var pageCount = 1;
    var totalCount = 0;
    $(document).ready(function () {
        createMemu();
        $("#btnSave").click(function () {
            if (ischeck()) {
                var id = $("#hidid").val();
                var modelid = $("#modelid").val();
                var title = $("#title").val();
                var content = $("#content").val();
                var memo = $("#memo").val();
                if (id > 0) {
                    operate("2", id, modelid, title, content, memo);
                }
                else {
                    operate("1", "0", modelid, title, content, memo);
                }
            }
        });
        $("#btnCancel").click(function () {
            reseting();
        });

    });
    function getContentMemu() {
        $(".manage_top_ul .manage_top_A").each(function () {
            weixinid = $(this).attr("typeid");
        })
        if (weixinid > 0) {
            gotoPage();
        }
    }
    //创建labs
    function createMemu() {
        $.ajax({
            type: "get",
            url: "@ST.GetTrendsFile("BaseConfiguration/WeixinUserMemu")" + "?random=" + Math.random(),
            async: true,
            data: { weixinid: "@ViewBag.weixinid" },
            dataType: "html",
            success: function (result) {
                $(".manage_top_ul").html(result);
                $(".manage_top_ul li").click(function () {
                    $(".manage_top_ul li").removeClass("manage_top_A");
                    $(this).addClass("manage_top_A");
                    reseting();
                    getContentMemu();
                })
                getContentMemu();
            }
        });
    }
    function ischeck() {
        var modelid = $("#modelid").val();
        if (modelid == "") {
            alert("模板id不能空，请重新输入！");
            $("#modelid").focus();
            return false;
        }
        var title = $("#title").val();
        if (title == "") {
            alert("模板标题不能空，请重新输入！");
            $("#title").focus();
            return false;
        }
        var content = $("#content").val();
        if (content == "") {
            alert("模板内容不能空，请重新输入！");
            $("#content").focus();
            return false;
        }
        return true;
    }

    var ajaxdata = {};
    function gotoPage() {
        ajaxdata["weixinid"] = weixinid;
        ajaxdata["pindex"] = pageIndex;
        ajaxdata["psize"] = pageSize;
        $.ajax({
            url: "@ST.GetTrendsFile("NewsModel/QueryNewsModel")",
            type: "POST",
            async: true,
            data: ajaxdata,
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                //iddenLoading();
            },
            dataType: 'json',

            success: function (data) {
                if (data != "" && data != null && data != undefined) {
                    totalCount = data.total;
                    var d = data.list;
                    var html = "";
                    if (d != "" && d != null && d != undefined) {
                        for (var i = 0; i < d.length; i++) {
                            html += "<tr>";
                            html += getTxtTd(d[i]["id"]) + getTxtTd(d[i]["modelid"]) + getTxtTd(d[i]["title"]) + getTxtTd(d[i]["remark"]);
                            html += "<td>" + getXqTd(d[i]["id"], weixinid) + "<a class=\"manage_bottom_font blue\" onclick=\"selected('" + d[i]["id"] + "','" + d[i]["modelid"] + "','" + d[i]["title"] + "','" + encodeURI(d[i]["content"]) + "','" + d[i]["remark"] + "')\" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(2, 3, ViewBag.leftid))'>编辑</a>";
                            html += "<a class=\"manage_bottom_font red\" onclick=\"operate('-1','" + d[i]["id"] + "')\" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(4, ViewBag.leftid))'>删除</a></td>";
                            html += '</tr>';
                        }
                    }
                    $("#dvList").html(html);
                    PageCountHtml();
                }
                else {
                    $("#dvList").html("没有查询到相关数据！");
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    function getTxtTd(s) {
        return '<td><span>' + s + '</span></td>';
    }
    function getXqTd(mid, weixinid) {
        //modelid = encodeURI(modelid);
        mid = encodeURI(mid);//主键id
        //title = encodeURI(title);
        //content = encodeURI(content);
        //memo = encodeURI(memo);
        weixinid = encodeURI(weixinid);
        var hrefvalue = "@ST.GetTrendsFile("NewsModel/NewsModelFieldList")" + "?mid=" + mid + "&weixinid=" + weixinid + "&leftid=@ViewBag.leftid";
        return '<a href="' + hrefvalue + '" class="manage_bottom_font red">详情</a>';
    }

    //编辑，删除，关闭操作
    function operate(op, id, modelid, title, content, memo) {
        $.ajax({
            url: "@ST.GetTrendsFile("NewsModel/UpdateNewsModel")",
            type: "POST",
            async: true,
            data: { op: op, id: id, modelid: modelid, title: title, content: content, memo: memo, weixinid: weixinid },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (data) {
                if (data == "1") {
                    gotoPage();
                    reseting();
                    alert("操作成功!");

                } else alert(data);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }



    //重置
    function reseting() {
        $("#hidid").val("0");
        $("#modelid").val("");
        $("#title").val("");
        $("#content").val("");
        $("#memo").val("");
    }
    //选择行
    function selected(id, modelid, title, content, memo) {
        $("#hidid").val(id);
        $("#modelid").val(modelid);
        $("#title").val(title);
        $("#content").val(decodeURI(content));
        $("#memo").val(memo);
    }
    //翻页
    function PageCountHtml() {
        pageCount = Math.ceil(totalCount / pageSize);
        if (pageCount == 0) {
            pageCount = 1;
        }
        $("#manage_page").html(PageHtml(pageIndex, pageCount));
        $("#manage_page .manage_page_right").click(function () {
            pageIndex = parseInt($("#txtPageNum").val()) + parseInt($(this).attr("tname"));
            $("#manage_page").html(PageHtml(pageIndex, pageCount));
            gotoPage();
        });
        $("#manage_page .tiaozhuan").click(function () {
            var pageNum = $("#txtPageNum").val();
            try {
                pageIndex = parseInt(pageNum);
            } catch (e) {
                alert("请输入数字");
                return false;
            }

            if (pageNum > 0 && pageNum <= pageCount) {
                $("#manage_page").html(PageHtml(pageIndex, pageCount));
                gotoPage();
            }
            else {
                alert("当前数字超出范围");
                return false;
            }
        });
    }
</script>
